package com.sample.FinalProject;

import java.util.ArrayList;
import java.util.Random;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.Rect;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TabHost;
import android.widget.TextView;

public class MenuActivity extends Activity implements OnClickListener{
	CustomApp ca;
	int isBuying=0;
	Button[] buttons=new Button[16];
	TextView[] texts=new TextView[10];
	TabHost tabhost1;
    TabHost.TabSpec specs[];
    FinalView fv;
    Random ran;
    Intent intentMain;
    int mode, temp2, temp3, troops, AiMode, AiGold, interval, AiTroops;
    int gold, xp, max_xp, level, str, intt, dex, con, skill_points, x, y, length, color, health, max_health, speed, age, damage;
	public void onCreate(Bundle savedInstanceState) {
		setContentView(R.layout.alt);
		 super.onCreate(savedInstanceState);	
		 Log.d("debug", "created2");
		 unpackBundle(getIntent().getBundleExtra("aBundle"));
		 setEverything();
		 ca=(CustomApp)this.getApplication();
		 fv=ca.getFinal();
		 intentMain = new Intent(MenuActivity.this , FinalProjectActivity.class);
		 ran=new Random();
	        specs=new TabHost.TabSpec[2];
	        tabhost1= (TabHost)findViewById(R.id.tabhost1);
	        tabhost1.setup();
	            specs[0]=tabhost1.newTabSpec("Character");
	            specs[0].setContent(R.id.tab1);
	            specs[1]=tabhost1.newTabSpec("Logistics");
	            specs[1].setContent(R.id.tab2);
	            specs[0].setIndicator("Character");
	            specs[1].setIndicator("Logistics");
	            tabhost1.addTab(specs[0]);
	            tabhost1.addTab(specs[1]);
	}
	public void onResume(){
		super.onResume();
		unpackBundle(getIntent().getBundleExtra("aBundle"));
		update();
	}
	public void onPause(){
	    super.onPause();
	    finish();
	    }
	public void onStop(){
	super.onStop();
	finish();
	}
	public void onClick(View v) {
		if(v.getId() == buttons[0].getId()){
		intentMain.putExtra("aBundle", makeReturnBundle());
		intentMain.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
		startActivity(intentMain);
		finish();
		}
		if(v.getId() == buttons[1].getId()){
		if(skill_points>0){
		str++;
		skill_points--;
		update();
		}
		else{
		texts[4].setText("Gold: " + Integer.toString(gold) + " Insufficient Skill Points");	
		}
		}

		if(v.getId() == buttons[2].getId()){
			if(skill_points>0){
				intt++;
				skill_points--;
				update();
				}
				else{
				texts[4].setText("Gold: " + Integer.toString(gold) + " Insufficient Skill Points");	
				}	
		}

		if(v.getId() == buttons[3].getId()){
			if(skill_points>0){
				con++;
				skill_points--;
				update();
				}
				else{
				texts[4].setText("Gold: " + Integer.toString(gold) + " Insufficient Skill Points");	
				}	
		}

		if(v.getId() == buttons[4].getId()){
			if(skill_points>0){
				dex++;
				skill_points--;
				update();
				}
				else{
				texts[4].setText("Gold: " + Integer.toString(gold) + " Insufficient Skill Points");	
				}	
		}

		if(v.getId() == buttons[5].getId()){
		//buying equipment	
		}

		if(v.getId() == buttons[6].getId()){
		fv.setMode(1);
		mode=1;
		texts[4].setText("Gold: " + Integer.toString(gold) + " Orders changed to Attack");		
		}

		if(v.getId() == buttons[7].getId()){
		fv.setMode(2);
		mode=2;	
		texts[4].setText("Gold: " + Integer.toString(gold) + " Orders changed to Defend");	
		}
		
		if(v.getId() == buttons[8].getId()){
		fv.setMode(3);	
		mode=3;	
		texts[4].setText("Gold: " + Integer.toString(gold) + " Orders changed to AutoPilot");
		}
		
		if(v.getId() == buttons[9].getId()){
		texts[4].setText("Gold: " + Integer.toString(gold) + " Orders changed to Group");
		fv.setMode(4);
		mode=4;	
		}

		if(v.getId() == buttons[10].getId()){
		if (gold>=1000-10*intt){			
		isBuying=1;	
		intentMain.putExtra("aBundle", makeReturnBundle());
		intentMain.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
		this.startActivity(intentMain);	
		finish();
		}
		else{
		texts[4].setText("Gold: " + Integer.toString(gold) + " Insufficient Gold");		
		}
		}

		if(v.getId() == buttons[11].getId()){
			if (gold>=2000-20*intt){			
				isBuying=2;	
				intentMain.putExtra("aBundle", makeReturnBundle());
				intentMain.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
				this.startActivity(intentMain);	
				finish();
			}
			else{
			texts[4].setText("Gold: " + Integer.toString(gold) + " Insufficient Gold");		
			}
		}

		if(v.getId() == buttons[12].getId()){
		if (gold>=100-intt && troops<5+intt){			
		troops++;
		gold-=100-intt;	
		texts[4].setText("Gold: " + Integer.toString(gold) + " Square Purchased");	
		ArrayList <Square> temp= ca.getSquares();	
		do{
		temp2=ran.nextInt(470);
		temp3=ran.nextInt(660);
		}
		while(fv.collisionCheck(new Square(temp2, temp3, Color.GREEN, false), -1, 0)==false || temp2<=270 || temp3<=500);	
		temp.add(new Square(temp2, temp3, Color.GREEN, false));
		ca.setSquares(temp);
		update();
		}
		else if (troops>=5+intt){
		texts[4].setText("Gold: " + Integer.toString(gold) + " Troop Cap Reached");		
		}
		else{
		texts[4].setText("Gold: " + Integer.toString(gold) + " Insufficient Gold");	
		}
		}

		if(v.getId() == buttons[13].getId()){
		if(gold>=1.5*(100-intt) && troops<5+intt){
		troops++;
		gold-=1.5*(100-intt);	
		texts[4].setText("Gold: " + Integer.toString(gold) + " Circle Purchased");
		ArrayList <Circle> temp= ca.getCircleUnits();	
		do{
		temp2=ran.nextInt(470);
		temp3=ran.nextInt(660);
		}
		while(fv.collisionCheck(new Circle(temp2, temp3, Color.GREEN, false), -1, 0)==false || temp2<=270 || temp3<=500);
		temp.add(new Circle(temp2, temp3, Color.GREEN, false));
		ca.setCircleUnits(temp);
		update();
		}
		else if (troops>=5+intt){
		texts[4].setText("Gold: " + Integer.toString(gold) + " Troop Cap Reached");		
		}
		else{
		texts[4].setText("Gold: " + Integer.toString(gold) + " Insufficient Gold");	
		}
		}

		if(v.getId() == buttons[14].getId()){
		if(gold>=5*(100-intt) && troops<5+intt){
			troops++;
			gold-=5*(100-intt);	
			texts[4].setText("Gold: " + Integer.toString(gold) + " Elite Square Purchased");
			ArrayList <Square> temp= ca.getSquares();	
			do{
			temp2=ran.nextInt(470);
			temp3=ran.nextInt(660);
			}
			while(fv.collisionCheck(new Square(temp2, temp3, Color.GREEN, false), -1, 0)==false || temp2<=270 || temp3<=500);
			temp.add(new Square(temp2, temp3, Color.BLUE, true));
			ca.setSquares(temp);
			update();
		}
		else if (troops>=5+intt){
		texts[4].setText("Gold: " + Integer.toString(gold) + " Troop Cap Reached");		
			}
		else{
		texts[4].setText("Gold: " + Integer.toString(gold) + " Insufficient Gold");		
		}
		}

		if(v.getId() == buttons[15].getId()){
			if(gold>=7.5*(100-intt) && troops<5+intt){
				troops++;
				gold-=7.5*(100-intt);	
				texts[4].setText("Gold: " + Integer.toString(gold) + " Elite Circle Purchased");
				ArrayList <Circle> temp= ca.getCircleUnits();	
				do{
				temp2=ran.nextInt(470);
				temp3=ran.nextInt(660);
				}
				while(fv.collisionCheck(new Circle(temp2, temp3, Color.GREEN, false), -1, 0)==false || temp2<=270 || temp3<=500);
				temp.add(new Circle(temp2, temp3, Color.BLUE, true));
				ca.setCircleUnits(temp);
				update();
				}
				else if (troops>=5+intt){
				texts[4].setText("Gold: " + Integer.toString(gold) + " Troop Cap Reached");		
				}
				else{
				texts[4].setText("Gold: " + Integer.toString(gold) + " Insufficient Gold");	
				}
		}
	}
	private Bundle makeReturnBundle(){
	Bundle bundle=new Bundle();
	bundle.putInt("gold", gold);
	bundle.putInt("x", x);	
	bundle.putInt("y", y);	
	bundle.putInt("str", str);	
	bundle.putInt("int", intt);	
	bundle.putInt("con", con);	
	bundle.putInt("dex", dex);	
	bundle.putInt("skill", skill_points);	
	bundle.putInt("mode", mode);
	bundle.putInt("troops", troops);
	bundle.putInt("isBuying", isBuying);
	bundle.putInt("AiGold", AiGold);	
	bundle.putInt("AiTroops", AiTroops);
	bundle.putInt("AiMode", AiMode);	
	bundle.putInt("interval", interval);
	bundle.putInt("XP", xp);
	bundle.putInt("MaxXP", max_xp);
	bundle.putInt("Health", health);
	bundle.putInt("MaxHealth", max_health);
	return bundle;	
	}
	private void unpackBundle(Bundle bundle){
	isBuying=0;	
	gold=bundle.getInt("gold");	
	x=bundle.getInt("x");	
	y=bundle.getInt("y");	
	str=bundle.getInt("str");	
	intt=bundle.getInt("int");	
	con=bundle.getInt("con");	
	dex=bundle.getInt("dex");	
	skill_points=bundle.getInt("skill");	
	mode=bundle.getInt("mode");
	troops=bundle.getInt("troops");
	AiGold=bundle.getInt("AiGold");
	AiTroops=bundle.getInt("AiTroops");
	AiMode=bundle.getInt("AiMode");
	interval=bundle.getInt("interval");
	xp=bundle.getInt("XP");
	max_xp=bundle.getInt("MaxXP");
	health=bundle.getInt("Health");
	max_health=bundle.getInt("MaxHealth");
	}
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	private void setEverything(){
		buttons[0]=(Button)findViewById(R.id.button1);
		buttons[1]=(Button)findViewById(R.id.button2);
		buttons[2]=(Button)findViewById(R.id.button3);
		buttons[3]=(Button)findViewById(R.id.button4);
		buttons[4]=(Button)findViewById(R.id.button5);
		buttons[5]=(Button)findViewById(R.id.button6);
		buttons[6]=(Button)findViewById(R.id.button7);
		buttons[7]=(Button)findViewById(R.id.button8);
		buttons[8]=(Button)findViewById(R.id.button9);
		buttons[9]=(Button)findViewById(R.id.button10);
		buttons[10]=(Button)findViewById(R.id.button11);
		buttons[11]=(Button)findViewById(R.id.button12);
		buttons[12]=(Button)findViewById(R.id.button13);
		buttons[13]=(Button)findViewById(R.id.button14);
		buttons[14]=(Button)findViewById(R.id.button15);
		buttons[15]=(Button)findViewById(R.id.button16);
		texts[0]=(TextView)findViewById(R.id.textView1);
		texts[1]=(TextView)findViewById(R.id.textView2);
		texts[2]=(TextView)findViewById(R.id.textView3);
		texts[3]=(TextView)findViewById(R.id.textView4);
		texts[4]=(TextView)findViewById(R.id.textView5);
		texts[5]=(TextView)findViewById(R.id.textView6);
		texts[6]=(TextView)findViewById(R.id.textView7);
		texts[7]=(TextView)findViewById(R.id.textView8);
		texts[8]=(TextView)findViewById(R.id.textView9);
		texts[9]=(TextView)findViewById(R.id.textView10);
		 for(int i=0; i<16; i++){
		        buttons[i].setOnClickListener(this);
			 }
	     buttons[0].setText("Back");	
	     texts[4].setText("Gold: " + Integer.toString(gold));
	     update();
	}
	private void update(){
		int temp2=intt+5;
		 texts[3].setText("Recruit: " + troops + "/" + temp2);
		 texts[0].setText("Stats: (Skill Points:" + Integer.toString(skill_points) + ")");
	     texts[5].setText("Strength: " + Integer.toString(str));
	     texts[6].setText("Intelligence: " + Integer.toString(intt));
	     texts[7].setText("Constitution: " + Integer.toString(con));
	     texts[8].setText("Dexterity: " + Integer.toString(dex));	
	     double temp=10*(100-intt);
	     buttons[10].setText("Buy Tower (" + temp + " gold)");
	     temp=20*(100-intt);
	     buttons[11].setText("Buy Fountain (" + temp + " gold)");
	     temp=1*(100-intt);
	     buttons[12].setText("Buy Square (" + temp + " gold)");
	     temp=1.5*(100-intt);
	     buttons[13].setText("Buy Circle (" + temp + " gold)");
	     temp=5*(100-intt);
	     buttons[14].setText("Buy Elite Square (" + temp + " gold)");
	     temp=7.5*(100-intt);
	     buttons[15].setText("Buy Elite Circle (" + temp + " gold)");
	}
	
}
